<?php
// 1903010328 引入资源
require "../vendor/autoload.php";
use PhpOffice\PhpSpreadsheet\IOFactory;
// 1903010328 获取文件对象
$file = "3-3.xlsx";
$fileSheet = IOFactory::load($file);
// 1903010328 获取第一张表
$workSheet = $fileSheet->getsheet(0);
// 获取最高的行号
$rows = $workSheet->getHighestRow();
// 1903010328 创建连接
include "../inc/data/conn.php";
// 1903010328 预处理的插入字符串
$sql = "insert into mamx_score
		(taskid,stuid,scnormal,scfinal)
		values
		(2,?,?,?)";
// 1903010328 预处理对象的创建
if (!($stmt = $conn->prepare($sql))) {
			# 失败
	die("失败！！！" );
}		
// 1903010328 获取表里的某列单元格
for ($i=2; $i <= $rows; $i++) { 
	# 读学号、姓名、班级
	 $stuId = $workSheet->getCell("A$i")->getValue();
	 $scNormal = $workSheet->getCell("D$i")->getValue();
	 $scFinal = $workSheet->getCell("G$i")->getValue();
	 $stmt -> bind_param("iss",$stuId,$scNormal,$scFinal);
	 $stmt -> execute();
	 if ($stmt -> affected_rows == 1) {
	 	# 成功
	 	$count += 1;
	 }
	// 1903010328 获取期末成绩
	// $stuexam[] = $workSheet->getCell("G$i")->getValue();
}
if ($count > 0) {
	# 写入成功
	echo <<<end
	<script>
	alert("成功录入{$count}个学生");
	
	</script>
end;	
}else{
	# 写入失败
	echo <<<end
	<script>
	alert("写入失败");
	
	</script>
end;	
}
// 1903010328 打印
// echo "学生的学号是：$stuid";
// var_dump($stuexam);